import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'common_widget.dart';

class MyDatePicker extends StatelessWidget {
  final String desc;
  final CupertinoDatePickerMode model;
  final DateTime initialDateTime;
  final DateTime minimumDate;
  final DateTime maximumDate;
  final ValueChanged<DateTime> onDateTimeChanged;

  MyDatePicker({
    this.desc = '请选择',
    this.model: CupertinoDatePickerMode.date,
    this.initialDateTime,
    this.minimumDate,
    this.onDateTimeChanged,
    this.maximumDate,
  });

  DateTime _dateTime;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 252.0,
      padding: const EdgeInsets.only(top: 0),
      color: CupertinoColors.white,
      child: DefaultTextStyle(
        style: const TextStyle(
          color: CupertinoColors.black,
          fontSize: 22.0,
        ),
        child: SafeArea(
          top: false,
          child: Column(
            children: [
              Container(
                width: double.infinity,
                height: 45,
                color: Color(0xfff8f8f8),
                child: Stack(
                  children: [
                    Container(
                      width: double.infinity,
                      height: 45,
                      child: Center(
                        child: Text(
                          desc,
                          style: TextStyle(
                            color: Color(0xff666666),
                            fontSize: 14,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                    Positioned(
                      child: MyInkWellWidget(
                        child: Center(
                          child: Text(
                            '确定',
                            style: TextStyle(
                              color: Color(0xffF78B30),
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                        onTap: () {
                          onDateTimeChanged(_dateTime ?? initialDateTime);
                          Navigator.pop(context);
                        },
                      ),
                      right: 20,
                      top: 0,
                      bottom: 0,
                    ),
                    Positioned(
                      child: MyInkWellWidget(
                        child: Center(
                          child: Text(
                            '取消',
                            style: TextStyle(
                              color: Color(0xffF78B30),
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                        onTap: () {
                          onDateTimeChanged(null);
                          Navigator.pop(context);
                        },
                      ),
                      left: 20,
                      top: 0,
                      bottom: 0,
                    ),
                  ],
                ),
              ),
              Expanded(
                child: CupertinoDatePicker(
                  minimumDate: minimumDate,
                  maximumDate: maximumDate,
                  mode: model,
                  use24hFormat: true,
                  initialDateTime: initialDateTime.isBefore(minimumDate)
                      ? minimumDate
                      : initialDateTime,
                  onDateTimeChanged: (DateTime newDateTime) =>
                      _dateTime = newDateTime,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
